package EJBTool.deployment.navigator;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserService;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class EJBToolNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider
{

	/**
	 * @generated
	 */
	static
	{
		EJBTool.deployment.part.EJBToolDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put(
						"Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		EJBTool.deployment.part.EJBToolDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put(
						"Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath)
	{
		Object element = elementPath.getLastSegment();
		if (element instanceof EJBTool.deployment.navigator.EJBToolNavigatorItem
				&& !isOwnView(((EJBTool.deployment.navigator.EJBToolNavigatorItem) element)
						.getView()))
		{
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element)
	{
		if (element instanceof EJBTool.deployment.navigator.EJBToolNavigatorGroup)
		{
			EJBTool.deployment.navigator.EJBToolNavigatorGroup group = (EJBTool.deployment.navigator.EJBToolNavigatorGroup) element;
			return EJBTool.deployment.part.EJBToolDiagramEditorPlugin
					.getInstance().getBundledImage(group.getIcon());
		}

		if (element instanceof EJBTool.deployment.navigator.EJBToolNavigatorItem)
		{
			EJBTool.deployment.navigator.EJBToolNavigatorItem navigatorItem = (EJBTool.deployment.navigator.EJBToolNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView()))
			{
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view)
	{
		switch (EJBTool.deployment.part.EJBToolVisualIDRegistry
				.getVisualID(view))
		{
			case EJBTool.deployment.edit.parts.DeplyomentViewEditPart.VISUAL_ID:
				return getImage(
						"Navigator?Diagram?http://jp.ac.u_tokyo.synclib?Root", EJBTool.deployment.providers.EJBToolElementTypes.Root_79); //$NON-NLS-1$
			case EJBTool.deployment.edit.parts.EJBEditPart.VISUAL_ID:
				return getImage(
						"Navigator?TopLevelNode?http://jp.ac.u_tokyo.synclib?EJB", EJBTool.deployment.providers.EJBToolElementTypes.EJB_1001); //$NON-NLS-1$
			case EJBTool.deployment.edit.parts.ModuleEditPart.VISUAL_ID:
				return getImage(
						"Navigator?TopLevelNode?http://jp.ac.u_tokyo.synclib?Module", EJBTool.deployment.providers.EJBToolElementTypes.Module_1002); //$NON-NLS-1$
			case EJBTool.deployment.edit.parts.EJBModuleEditPart.VISUAL_ID:
				return getImage(
						"Navigator?Link?http://jp.ac.u_tokyo.synclib?EJB?Module", EJBTool.deployment.providers.EJBToolElementTypes.EJBModule_3001); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType)
	{
		ImageRegistry imageRegistry = EJBTool.deployment.part.EJBToolDiagramEditorPlugin
				.getInstance().getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null
				&& elementType != null
				&& EJBTool.deployment.providers.EJBToolElementTypes
						.isKnownElementType(elementType))
		{
			image = EJBTool.deployment.providers.EJBToolElementTypes
					.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null)
		{
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element)
	{
		if (element instanceof EJBTool.deployment.navigator.EJBToolNavigatorGroup)
		{
			EJBTool.deployment.navigator.EJBToolNavigatorGroup group = (EJBTool.deployment.navigator.EJBToolNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof EJBTool.deployment.navigator.EJBToolNavigatorItem)
		{
			EJBTool.deployment.navigator.EJBToolNavigatorItem navigatorItem = (EJBTool.deployment.navigator.EJBToolNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView()))
			{
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view)
	{
		if (view.getElement() != null && view.getElement().eIsProxy())
		{
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (EJBTool.deployment.part.EJBToolVisualIDRegistry
				.getVisualID(view))
		{
			case EJBTool.deployment.edit.parts.DeplyomentViewEditPart.VISUAL_ID:
				return getRoot_79Text(view);
			case EJBTool.deployment.edit.parts.EJBEditPart.VISUAL_ID:
				return getEJB_1001Text(view);
			case EJBTool.deployment.edit.parts.ModuleEditPart.VISUAL_ID:
				return getModule_1002Text(view);
			case EJBTool.deployment.edit.parts.EJBModuleEditPart.VISUAL_ID:
				return getEJBModule_3001Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getRoot_79Text(View view)
	{
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getEJB_1001Text(View view)
	{
		IAdaptable hintAdapter = new EJBTool.deployment.providers.EJBToolParserProvider.HintAdapter(
				EJBTool.deployment.providers.EJBToolElementTypes.EJB_1001,
				(view.getElement() != null ? view.getElement() : view),
				EJBTool.deployment.part.EJBToolVisualIDRegistry
						.getType(EJBTool.deployment.edit.parts.EJBNameEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null)
		{
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		}
		else
		{
			EJBTool.deployment.part.EJBToolDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 4001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getModule_1002Text(View view)
	{
		IAdaptable hintAdapter = new EJBTool.deployment.providers.EJBToolParserProvider.HintAdapter(
				EJBTool.deployment.providers.EJBToolElementTypes.Module_1002,
				(view.getElement() != null ? view.getElement() : view),
				EJBTool.deployment.part.EJBToolVisualIDRegistry
						.getType(EJBTool.deployment.edit.parts.ModuleNameEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null)
		{
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		}
		else
		{
			EJBTool.deployment.part.EJBToolDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 4003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getEJBModule_3001Text(View view)
	{
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view)
	{
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view)
	{
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig)
	{}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento)
	{}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento)
	{}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement)
	{
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view)
	{
		return EJBTool.deployment.edit.parts.DeplyomentViewEditPart.MODEL_ID
				.equals(EJBTool.deployment.part.EJBToolVisualIDRegistry
						.getModelID(view));
	}

}
